package secretary;

import home.HomePage;
import president.PresidentHome;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Event;

public class ManageEvents extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	public ManageEvents() throws IOException, SQLException
	{
		add(new BookmarkablePageLink("home", SecretaryHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("newEvent", AddEvent.class));
		
		Form form = new Form("manageEventForm");
		
		List<Event> events = (List<Event>)sqlMap.queryForList("Event.getAll");
		
		ListView eachEvent = new ListView("eachEvent",events) {
			private static final long serialVersionUID = 1L;
			
			protected void populateItem(ListItem item) {
				final Event event = (Event) item.getModelObject();
				item.add(new CustomCheckBox("checkbox", new CustomCheckBoxModel(event.getEventid())));
				item.add(new Label("eventid", Integer.toString(event.getEventid())));
				item.add(new Label("etitle", event.getEvent_title()));
				item.add(new Label("uploadedBy", event.getUpload_by()));
				item.add(new Label("uploadedDate", event.getUpload_date()));
			}
		};
		form.add(eachEvent);
		add(form);
	}

	public class CustomCheckBox extends CheckBox{
		private static final long serialVersionUID = 1L;

		public CustomCheckBox(String id, IModel model) {
			super(id, model);
		}
		
		protected boolean wantOnSelectionChangedNotifications() {
			return false;
		}
	}

	private final List<Integer> selections = new ArrayList();
	
	public class CustomCheckBoxModel implements IModel, Serializable{
		private static final long serialVersionUID = 1L;
		private final int id;
		
		public CustomCheckBoxModel(int id) {
			super();
			this.id = id;
		}

		public IModel getNestedModel() {
			return null;
		}

		public Object getObject(){
			return isSelected();
		}
		
		private Boolean isSelected() {
			if (selections.contains(id))
				return true;
			else
				return false;
		}
			
		public void setObject(Object object){
			boolean selected = ((Boolean) object).booleanValue();
			boolean previouslySelected = isSelected();
			if (selected){
				if (!previouslySelected) {
					selections.add(id);
				}
			} else {
				if (previouslySelected) {
					selections.remove((Integer)id);
				}
			}
		}
		public void detach() {}
	}

}
